/**
 * 协议模板定义
 * 包含常用的能源管理设备协议模板
 * 
 * 数据点参数说明:
 * - name: 数据点英文标识符，用于程序内部引用
 * - chinese_name: 数据点中文名称，用于界面显示
 * - address: Modbus寄存器地址 (0-65535)
 * - register_type: 寄存器类型 "holding"(保持寄存器) 或 "input"(输入寄存器)
 * - length: 占用的寄存器个数 (1-4个寄存器)
 * - data_type: 数据类型 uint16/int16/uint32/int32/float32
 * - byte_order: 字节序 "big_endian"(大端) 或 "little_endian"(小端)
 * - coefficient: 数据转换系数，原始值乘以系数得到实际值
 * - offset: 数据偏移量，用于数据校准
 * - unit: 数据单位，如V/A/kW/Hz/℃等
 * - range: 数据有效范围 {min: 最小值, max: 最大值}
 * - decimal_places: 显示小数位数
 * - description: 数据点详细描述
 * - bit_field: (可选)位字段定义，用于状态位图数据
 */

export const protocolTemplates = {
  energy_meter: {
    name: "能源设备协议模板",
    description: "标准能源管理设备Modbus协议模板，包含电压、电流、功率等常用数据点",
    protocol_info: {
      vendor: "厂家名称",
      model: "设备型号", 
      version: "协议版本",
      description: "协议描述"
    },
    connection: {
      default_slave_id: 1,
      default_timeout: 3000,
      retry_times: 3,
      retry_interval: 1000
    },
    data_points: [
      {
        name: "grid_voltage_A",
        chinese_name: "电网电压A相",
        address: 1900,
        register_type: "holding",
        length: 2,
        data_type: "uint32",
        byte_order: "big_endian",
        coefficient: 0.001,
        offset: 0,
        unit: "V",
        range: {
          min: 0,
          max: 655.35
        },
        decimal_places: 2,
        description: "电网A相电压，AB线电压"
      },
      {
        name: "grid_current_A",
        chinese_name: "电网电流A相",
        address: 1902,
        register_type: "holding",
        length: 2,
        data_type: "int32",
        byte_order: "big_endian",
        coefficient: 0.001,
        offset: 0,
        unit: "A",
        range: {
          min: -1000,
          max: 1000
        },
        decimal_places: 3,
        description: "电网A相电流"
      },
      {
        name: "total_active_power",
        chinese_name: "总有功功率",
        address: 1910,
        register_type: "holding",
        length: 2,
        data_type: "int32",
        byte_order: "big_endian",
        coefficient: 0.1,
        offset: 0,
        unit: "kW",
        range: {
          min: -10000,
          max: 10000
        },
        decimal_places: 1,
        description: "系统总有功功率"
      },
      {
        name: "battery_soc",
        chinese_name: "电池电量",
        address: 2000,
        register_type: "holding",
        length: 1,
        data_type: "uint16",
        byte_order: "big_endian",
        coefficient: 0.1,
        offset: 0,
        unit: "%",
        range: {
          min: 0,
          max: 100
        },
        decimal_places: 1,
        description: "电池剩余电量百分比"
      },
      {
        name: "running_status",
        chinese_name: "运行状态",
        address: 2100,
        register_type: "holding",
        length: 1,
        data_type: "uint16",
        byte_order: "big_endian",
        coefficient: 1,
        offset: 0,
        unit: "",
        bit_field: {
          bit0: "停机",
          bit1: "运行",
          bit2: "故障",
          bit3: "维护模式"
        },
        description: "设备运行状态位图"
      },
      {
        name: "device_temperature",
        chinese_name: "设备温度",
        address: 2200,
        register_type: "input",
        length: 1,
        data_type: "int16",
        byte_order: "big_endian",
        coefficient: 0.1,
        offset: -40,
        unit: "℃",
        range: {
          min: -40,
          max: 125
        },
        decimal_places: 1,
        description: "设备内部温度"
      },
      {
        name: "daily_energy",
        chinese_name: "日发电量",
        address: 3000,
        register_type: "holding",
        length: 2,
        data_type: "uint32",
        byte_order: "little_endian",
        coefficient: 0.01,
        offset: 0,
        unit: "kWh",
        range: {
          min: 0,
          max: 999999.99
        },
        decimal_places: 2,
        description: "当日累计发电量"
      },
      {
        name: "power_factor",
        chinese_name: "功率因数",
        address: 3100,
        register_type: "holding",
        length: 1,
        data_type: "int16",
        byte_order: "big_endian",
        coefficient: 0.001,
        offset: 0,
        unit: "",
        range: {
          min: -1,
          max: 1
        },
        decimal_places: 3,
        description: "系统功率因数"
      },
      {
        name: "grid_frequency",
        chinese_name: "电网频率",
        address: 3200,
        register_type: "holding",
        length: 1,
        data_type: "uint16",
        byte_order: "big_endian",
        coefficient: 0.01,
        offset: 0,
        unit: "Hz",
        range: {
          min: 45,
          max: 55
        },
        decimal_places: 2,
        description: "电网频率"
      },
      {
        name: "dc_voltage",
        chinese_name: "直流电压",
        address: 4000,
        register_type: "holding",
        length: 2,
        data_type: "float32",
        byte_order: "big_endian",
        coefficient: 1,
        offset: 0,
        unit: "V",
        range: {
          min: 0,
          max: 1500
        },
        decimal_places: 2,
        description: "直流侧电压"
      }
    ],
    function_codes: {
      read_holding_registers: "0x03",
      read_input_registers: "0x04",
      write_single_register: "0x06",
      write_multiple_registers: "0x10"
    },
    error_codes: {
      "0x01": "非法功能码",
      "0x02": "非法数据地址",
      "0x03": "非法数据值",
      "0x04": "从站设备故障"
    }
  },

  modbus_basic: {
    name: "基础Modbus模板",
    description: "基础Modbus协议模板，包含基本的寄存器配置",
    protocol_info: {
      vendor: "通用厂家",
      model: "Modbus设备",
      version: "1.0.0",
      description: "基础Modbus协议模板"
    },
    connection: {
      default_slave_id: 1,
      default_timeout: 3000,
      retry_times: 3,
      retry_interval: 1000
    },
    data_points: [
      {
        name: "register_01",
        chinese_name: "寄存器01",
        address: 1,
        register_type: "holding",
        length: 1,
        data_type: "uint16",
        byte_order: "big_endian",
        coefficient: 1,
        offset: 0,
        unit: "",
        range: {
          min: 0,
          max: 65535
        },
        decimal_places: 0,
        description: "保持寄存器01"
      },
      {
        name: "register_02",
        chinese_name: "寄存器02",
        address: 2,
        register_type: "holding",
        length: 1,
        data_type: "uint16",
        byte_order: "big_endian",
        coefficient: 1,
        offset: 0,
        unit: "",
        range: {
          min: 0,
          max: 65535
        },
        decimal_places: 0,
        description: "保持寄存器02"
      },
      {
        name: "input_01",
        chinese_name: "输入寄存器01",
        address: 1001,
        register_type: "input",
        length: 1,
        data_type: "uint16",
        byte_order: "big_endian",
        coefficient: 1,
        offset: 0,
        unit: "",
        range: {
          min: 0,
          max: 65535
        },
        decimal_places: 0,
        description: "输入寄存器01"
      }
    ],
    function_codes: {
      read_holding_registers: "0x03",
      read_input_registers: "0x04", 
      write_single_register: "0x06",
      write_multiple_registers: "0x10"
    },
    error_codes: {
      "0x01": "非法功能码",
      "0x02": "非法数据地址",
      "0x03": "非法数据值"
    }
  },

  industrial_plc: {
    name: "工控PLC模板",
    description: "工业PLC设备协议模板，包含常用的工控数据点",
    protocol_info: {
      vendor: "工控厂家",
      model: "PLC控制器",
      version: "1.0.0",
      description: "工业PLC设备协议"
    },
    connection: {
      default_slave_id: 1,
      default_timeout: 5000,
      retry_times: 3,
      retry_interval: 2000
    },
    data_points: [
      {
        name: "analog_input_01",
        chinese_name: "模拟输入01",
        address: 10001,
        register_type: "input",
        length: 1,
        data_type: "uint16",
        byte_order: "big_endian",
        coefficient: 0.1,
        offset: 0,
        unit: "V",
        range: {
          min: 0,
          max: 10
        },
        decimal_places: 1,
        description: "模拟输入通道01"
      },
      {
        name: "digital_output_01",
        chinese_name: "数字输出01",
        address: 20001,
        register_type: "holding",
        length: 1,
        data_type: "uint16",
        byte_order: "big_endian",
        coefficient: 1,
        offset: 0,
        unit: "",
        range: {
          min: 0,
          max: 1
        },
        decimal_places: 0,
        description: "数字输出通道01"
      },
      {
        name: "counter_value",
        chinese_name: "计数器值",
        address: 30001,
        register_type: "holding",
        length: 2,
        data_type: "uint32",
        byte_order: "big_endian",
        coefficient: 1,
        offset: 0,
        unit: "",
        range: {
          min: 0,
          max: 4294967295
        },
        decimal_places: 0,
        description: "累计计数器"
      }
    ],
    function_codes: {
      read_holding_registers: "0x03",
      read_input_registers: "0x04",
      write_single_register: "0x06",
      write_multiple_registers: "0x10"
    },
    error_codes: {
      "0x01": "非法功能码",
      "0x02": "非法数据地址", 
      "0x03": "非法数据值",
      "0x05": "设备忙",
      "0x06": "设备离线"
    }
  }
}

/**
 * 根据协议分类获取推荐的模板
 * @param {string} category - 协议分类
 * @returns {object} 协议模板对象
 */
export const getTemplateByCategory = (category) => {
  const categoryMap = {
    energy: 'energy_meter',
    industrial: 'industrial_plc',
    modbus: 'modbus_basic',
    custom: 'energy_meter'
  }
  return protocolTemplates[categoryMap[category]] || protocolTemplates.energy_meter
}

/**
 * 获取所有可用模板的列表
 * @returns {Array} 模板列表
 */
export const getTemplateList = () => {
  return Object.keys(protocolTemplates).map(key => ({
    key,
    name: protocolTemplates[key].name,
    description: protocolTemplates[key].description,
    dataPointCount: protocolTemplates[key].data_points.length
  }))
}

/**
 * 根据模板key获取完整模板
 * @param {string} templateKey - 模板键值
 * @returns {object} 协议模板对象
 */
export const getTemplate = (templateKey) => {
  return protocolTemplates[templateKey] || protocolTemplates.energy_meter
}